package com.wva.es.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.elasticsearch.core.query.UpdateResponse;

import java.util.Map;

/**
 * 对data-elasticsearch进行封装的工具类
 * @Author: hong
 * @Date: 2025/3/1 17:48:24
 */
public class DataElasticsearchUtils {
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate ;

    /**
     * 增量修改
     * @param params 修改哪些字段
     * @param id 主键
     * @param index 索引
     * @return
     */
    public UpdateResponse update(Map<String, Object> params, String id, String index) {
        UpdateQuery updateQuery = UpdateQuery.builder(id) // 文档ID
                .withDocument(Document.from(params)) // 更新字段
                .build();
        return elasticsearchRestTemplate.update(
                updateQuery,
                IndexCoordinates.of(index)
        );
    }
}
